package 单调栈;

import java.util.HashMap;
import java.util.Stack;

public class Solution20 {

    public static void main(String[] args) {

        System.out.println(isValid("([}}])"));
    }

    public static boolean isValid(String s) {
        char[] array = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        if (array.length % 2 != 0) {
            return false;
        }
        for (int i = 0; i < array.length; i++) {
            char c = array[i];
            if (String.valueOf(c).equals("(")) {
                stack.push(c);
            }
            if (String.valueOf(c).equals("[")) {
                stack.push(c);
            }
            if (String.valueOf(c).equals("{")) {
                stack.push(c);
            }
            if (String.valueOf(c).equals("}")) {
                if (!stack.isEmpty()) {
                    Character pop = stack.peek();
                    if (pop.toString().equals("{")) {
                        stack.pop();
                    } else {
                        stack.push(c);
                    }
                } else {
                    stack.push(c);
                }

            }
            if (String.valueOf(c).equals(")")) {
                if (!stack.isEmpty()) {
                    Character pop = stack.peek();
                    if (pop.toString().equals("(")) {
                        stack.pop();
                    } else {
                        stack.push(c);
                    }
                } else {
                    stack.push(c);

                }
            }
            if (String.valueOf(c).equals("]")) {
                if (!stack.isEmpty()) {
                    Character pop = stack.peek();
                    if (pop.toString().equals("[")) {
                        stack.pop();
                    } else {
                        stack.push(c);
                    }
                } else {
                    stack.push(c);

                }
            }
        }
        if (stack.isEmpty()) {
            return true;
        } else {
            return false;
        }


    }
}
